package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addBed(BedDto bedDto) {
        //把dto对象转换成实体类对象
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);//属性名称 一致 自动封装数据
        bed.setCreateBy(1L);
        bed.setCreateTime(LocalDateTime.now());
        bed.setBedStatus(0);

        //调用 mapper 实现数据的保存
        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_NUMBER_REPETITION);
        }
    }

    @Override
    public BedVo getByIdBed(Long id) {
        Bed bed = bedMapper.getByIdBed(id);
        BedVo bedVo = BeanUtil.copyProperties(bed, BedVo.class);

        return bedVo;
    }

    @Override
    public void updateBed(BedDto bedDto) {
        //数据类型转换
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);

        log.info("修改床位:{}",bed);

        //补全属性
        bed.setUpdateBy(1L);
        bed.setUpdateTime(LocalDateTime.now());

        //mapper 执行更新操作
        bedMapper.updateBed(bed);
    }

    @Override
    public void deleteBed(Long id) {
        bedMapper.deleteBed(id);
    }


}

